//
//  AMNoticeCardTableViewCell.m
//  GYWBaseAPP
//
//  Created by 郭艺伟 on 16/5/25.
//  Copyright © 2016年 郭艺伟. All rights reserved.
//

#import "AMNoticeCardTableViewCell.h"

#import <Masonry/Masonry.h>

#define kTypeTitleLabelFontSize 14

#define kTitleLabelFontSize 18

#define kDesContentLabelFontSize 16


#define kBuildTimeLabelFontSize 16

#define kButtonHeight 16
#define kButtonWidth 40

#define kMarginX 10
#define KMarginY 5


@interface AMNoticeCardTableViewCell ()

/**
 *  cell所有子视图的父视图
 */
@property (nonatomic, weak) UIView *cellBGView;

/**
 *  公告类型标题
 */
@property (nonatomic, strong) UILabel *typeTitleLabel;


/**
 *  文本内容BGview
 */
@property (nonatomic, weak) UIView *contentBGView;

/**
 *  置顶图标
 */
@property (nonatomic, strong) UIImageView *topImageView;

/**
 *  标题
 */
@property (nonatomic, strong) UILabel *titleLabel;


/**
 *  描述标题
 */
@property (nonatomic, strong) UILabel *desContentLabel;


/**
 *  时间
 */
@property (nonatomic, strong) UILabel *buildTimeLabel;


/**
 *  投票按钮
 */
@property (nonatomic, strong) UIButton *voteBtn;

/**
 * 附件按钮
 */
@property (nonatomic, strong) UIButton *annexBtn;


/**
 *  评论按钮
 */
@property (nonatomic, strong) UIButton *commentBtn;

@property (nonatomic, strong) MASConstraint *titleLabelConstraint_Left;


@property (nonatomic, strong) MASConstraint *annexBtnConstraint_right;

@property (nonatomic, strong) MASConstraint *voteBtnConstraint_right;
@end

@implementation AMNoticeCardTableViewCell


+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"AMNoticeCardTableViewCell";
    AMNoticeCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[AMNoticeCardTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        //1.设置cell的背景颜色背景图
        self.backgroundColor = [UIColor clearColor];
        self.backgroundView = nil;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        //2.添加所有子视图
        [self setupAllSupViews];
    }
    return self;
}

-(void)setupAllSupViews
{
    //1.cellBGView(cell所有子视图的父视图)
    UIView *cellBGView = [[UIView alloc] init];
    cellBGView.backgroundColor = [UIColor whiteColor];
    cellBGView.layer.masksToBounds = YES;
    [self.contentView addSubview:cellBGView];
    self.cellBGView = cellBGView;
    
    
    //1.公告类型标题
    UILabel *typeTitleLabel = [[UILabel alloc] init];
    typeTitleLabel.backgroundColor = cellBGView.backgroundColor;
    typeTitleLabel.backgroundColor = [UIColor redColor];
    typeTitleLabel.font = [UIFont systemFontOfSize:kTypeTitleLabelFontSize];
    [cellBGView addSubview:typeTitleLabel];
    _typeTitleLabel= typeTitleLabel;
    _typeTitleLabel.text = @"通知公告";
    
    
    
    //2.置顶标识
    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.backgroundColor = [UIColor orangeColor];
    [cellBGView addSubview:topImageView];
    _topImageView = topImageView;
    
    
    //3.公告标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.backgroundColor = cellBGView.backgroundColor;
    titleLabel.backgroundColor = [UIColor redColor];
    titleLabel.font = [UIFont systemFontOfSize:kTitleLabelFontSize];
    [cellBGView addSubview:titleLabel];
    _titleLabel= titleLabel;
    _titleLabel.text = @"关于2016年无法登陆99u的紧急公告";
    
    
    //4.公告描述标题
    UILabel *desContentLabel = [[UILabel alloc] init];
    desContentLabel.backgroundColor = cellBGView.backgroundColor;
    desContentLabel.backgroundColor = [UIColor redColor];
    desContentLabel.numberOfLines = 0;
    desContentLabel.font = [UIFont systemFontOfSize:kDesContentLabelFontSize];
    [cellBGView addSubview:desContentLabel];
    _desContentLabel= desContentLabel;
    _desContentLabel.text = @"关于2016年无法登陆99u的紧急公告疯狂的反馈的喝咖啡和考试的回复的看法和快递费还灯火疯狂的回复大客户交电话费交电话费——》肯定会付电话费的和的恢复快打回访客户的付电话费的客户反馈的合法化的电话1234567890!";
    
    
    //5.时间
    UILabel *buildTimeLabel = [[UILabel alloc] init];
    buildTimeLabel.backgroundColor = cellBGView.backgroundColor;
    buildTimeLabel.backgroundColor = [UIColor redColor];
    buildTimeLabel.font = [UIFont systemFontOfSize:kBuildTimeLabelFontSize];
    [cellBGView addSubview:buildTimeLabel];
    _buildTimeLabel= buildTimeLabel;
    _buildTimeLabel.text = @"8月14日 18:30";
    
    
    //6.投票按钮
    UIButton *voteBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    voteBtn.backgroundColor = [UIColor orangeColor];
    [cellBGView addSubview:voteBtn];
    _voteBtn = voteBtn;
    
    
    //7.投票按钮
    UIButton *annexBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    annexBtn.backgroundColor = [UIColor grayColor];
    [cellBGView addSubview:annexBtn];
    _annexBtn = annexBtn;
    
    //8.评论按钮
    UIButton *commentBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    commentBtn.backgroundColor = [UIColor yellowColor];
    [cellBGView addSubview:commentBtn];
    _commentBtn = commentBtn;
    
    
    
    [self.cellBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.typeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cellBGView).offset(KMarginY);
        make.left.equalTo(self.cellBGView).offset(kMarginX);
        make.right.equalTo(self.cellBGView).offset(-kMarginX);
        make.height.mas_equalTo(kTypeTitleLabelFontSize+3);
    }];
    
    
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeTitleLabel.mas_bottom).offset(KMarginY*2);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.bottom.equalTo(self.typeTitleLabel.mas_bottom).offset(30);
        
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeTitleLabel.mas_bottom).offset(KMarginY*2);
        _titleLabelConstraint_Left = make.left.equalTo(self.cellBGView).offset(2*kMarginX +20);
        make.left.equalTo(self.topImageView.mas_right).offset(kMarginX);
        make.right.equalTo(self.cellBGView).offset(-kMarginX);
        make.height.mas_equalTo(kTitleLabelFontSize+3);
    }];
    
    [self.desContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(KMarginY);
        make.left.equalTo(self.cellBGView).offset(kMarginX);
        make.right.equalTo(self.cellBGView).offset(-kMarginX);
        make.height.mas_lessThanOrEqualTo((kDesContentLabelFontSize+3+kDesContentLabelFontSize*0.2)*2).priorityHigh();
    }];

    
    [self.buildTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.desContentLabel.mas_bottom).offset(KMarginY*2);
        make.left.equalTo(self.cellBGView).offset(kMarginX);
        make.height.mas_lessThanOrEqualTo(kBuildTimeLabelFontSize +3);
        make.bottom.equalTo(self.cellBGView);
    }];

    

    

    
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.desContentLabel.mas_bottom).offset(KMarginY*2);
        make.right.equalTo(self.cellBGView).offset(-kMarginX);
        make.height.mas_equalTo(kButtonHeight);
        make.width.mas_equalTo(kButtonWidth);
    }];

    [self.annexBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.desContentLabel.mas_bottom).offset(KMarginY*2);
        _annexBtnConstraint_right = make.right.equalTo(self.cellBGView).offset(-2*kMarginX-kButtonWidth);
        make.height.mas_equalTo(kButtonHeight);
        make.width.mas_equalTo(kButtonWidth);
    }];
    
    [self.voteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.desContentLabel.mas_bottom).offset(KMarginY*2);
        _voteBtnConstraint_right = make.right.equalTo(self.cellBGView).offset(-3*kMarginX-2*kButtonWidth);
        make.height.mas_equalTo(kButtonHeight);
        make.width.mas_equalTo(kButtonWidth);
    }];
    
}


- (void)setNoticeCard:(AMNoticeCard *)noticeCard
{
    _noticeCard = noticeCard;
    
    self.typeTitleLabel.text = noticeCard.typeTitle;
    
    if (noticeCard.isTop) {
        self.topImageView.hidden = NO;
        self.titleLabelConstraint_Left.offset(kMarginX*2 +20);
        
    }else{
        self.topImageView.hidden = YES;
        self.titleLabelConstraint_Left.offset(kMarginX);
    }
    
    self.titleLabel.text = noticeCard.title;
    
    self.desContentLabel.text = noticeCard.desContent;
    
    self.buildTimeLabel.text = noticeCard.time;
    

    

    

    
    if (noticeCard.isShowCommentBtn) {
        self.commentBtn.hidden = NO;
        [self.commentBtn setTitle:[NSString stringWithFormat:@"%ld",noticeCard.commentCount] forState:UIControlStateNormal];
    }else{
        self.commentBtn.hidden = YES;
        [self.commentBtn setTitle:nil forState:UIControlStateNormal];
    }
    
    if (noticeCard.isShowAnnexBtn) {
        self.annexBtn.hidden = NO;
        [self.annexBtn setTitle:[NSString stringWithFormat:@"%ld",noticeCard.annexCount] forState:UIControlStateNormal];
    }else{
        self.annexBtn.hidden = YES;
        [self.annexBtn setTitle:nil forState:UIControlStateNormal];
    }
    
    if (noticeCard.isShowVoteBtn) {
        self.voteBtn.hidden = NO;
        [self.voteBtn setTitle:[NSString stringWithFormat:@"%ld",noticeCard.voteCount] forState:UIControlStateNormal];
    }else{
        self.voteBtn.hidden = YES;
        [self.voteBtn setTitle:nil forState:UIControlStateNormal];
    }
    
    
    
    if (noticeCard.isShowCommentBtn) {
        //显示评论按钮
        
        if (noticeCard.isShowAnnexBtn) {
            //显示附件按钮
            self.annexBtnConstraint_right.offset(-2 *kMarginX - kButtonWidth);
            if (noticeCard.isShowVoteBtn) {
                self.voteBtnConstraint_right.offset(-3 *kMarginX - 2*kButtonWidth);
            }
        }else{
            //不显示附件按钮
            if (noticeCard.isShowVoteBtn) {
                //显示投票按钮
                self.voteBtnConstraint_right.offset(-2 *kMarginX - kButtonWidth);
            }
            
        }
        
    }else{
        //不显示评论按钮
        if (noticeCard.isShowAnnexBtn) {
            //显示附件按钮
            self.annexBtnConstraint_right.offset(-kMarginX);
            if (noticeCard.isShowVoteBtn) {
                //显示投票按钮
                self.voteBtnConstraint_right.offset(-2 *kMarginX - kButtonWidth);
            }
        }else{
            //不显示附件按钮
            if (noticeCard.isShowVoteBtn) {
                //显示投票按钮
                self.voteBtnConstraint_right.offset(-kMarginX);
            }
            
        }
        
        
    }
    
    
}

@end
